/**
 * 
 */
package com.nextive.interview.util;

import static com.nextive.interview.activity.view.DefaultTextValuesHolder.fetchValueFor;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.nextive.interview.R;

import android.text.format.DateUtils;

/**
 * This class contains several method needed when dealing with dates.
 * @author Cesar Roman
 *
 */
public class DateUtil {
	
	// date and time format
	private static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy");
	private static final DateFormat TIME_FORMAT = new SimpleDateFormat("hh:mm a");
	
	/**
	 * It generates a visual date given its calendar. This is done via <code>DATE_FORMAT</code>.
	 * @param calendar
	 * @return visual date
	 */
	public static String generateVisualDate(Calendar calendar) {
		
		return generateVisualDate(calendar, true);
		
	}
	
	/**
	 * It generates a visual date given its calendar. This is done via <code>DATE_FORMAT</code>.
	 * @param calendar
	 * @param showToday true if 'Today' has to be shown
	 * @return visual date
	 */
	public static String generateVisualDate(Calendar calendar, boolean showToday) {
		
		if (showToday && DateUtils.isToday(calendar.getTimeInMillis()))
			return fetchValueFor(R.string.today).toString();
		
		return DATE_FORMAT.format(calendar.getTime());
		
	}

	/**
	 * It generates a visual time given its calendar. This is done via <code>DATE_FORMAT</code>.
	 * @param calendar
	 * @return visual time
	 */
	public static String generateVisualTime(Calendar calendar) {
		
		return TIME_FORMAT.format(calendar.getTime());
		
	}
	
	/**
	 * It generates a calendar from given milliseconds.
	 * @param millis
	 * @return the calendar
	 */
	public static Calendar generateCalendar(long millis) {
		
		Calendar result = Calendar.getInstance();
		result.setTimeInMillis(millis);
		
		return result;
		
	}
	
	/**
	 * It determines if given date is future or not.
	 * @param calendar date to check
	 * @return true if future
	 */
	public static boolean isFuture(Calendar calendar) {
		
		return calendar.getTimeInMillis() > Calendar.getInstance().getTimeInMillis();
		
	}
	
}